For Programmers familiar with ACM API? Drawing Initials [closed]
Posted
by
user71992
on Programmers
See other posts from Programmers
or by user71992
Published on 2012-11-07T09:36:13Z
Indexed on
2012/11/07
11:20 UTC
Read the original article
Hit count: 354
java
Possible Duplicate:
For Programmers familiar with ACM API? Drawing Initials
I came across an exercise (in the book "The Art and Science of Java" by Eric Roberts) that requires using only GArc and GLine classes to create a lettering library which draws your initials on the canvas. This should be made independent of the GLabel class.
I'd like to know the correct approach to use in solving this problem. I'm not sure what I have so far is good enough (I'm thinking it's too long). The questions requires that I use a good Top-Down approach.
Here's my code so far:
//Passes letters to GLetter objects and draws them on the canvas
package artScienceJavaExercises.chapter8;
import acm.program.*;
//import acm.graphics.*;
public class DrawInitials extends GraphicsProgram{
public void init(){
resize(400,400);
}
public void run(){
//String let = readLine("Letter?: ");
letter = new GLetter("l");
add(letter, (getWidth()-letter.getWidth()*2)/2, (getHeight()-letter.getHeight())/2);
add(new GLetter("o"), (letter.getX()+letter.getWidth()), letter.getY());
}
private GLetter letter;
}
//GLetter Class
package artScienceJavaExercises.chapter8;
import acm.graphics.*;
import java.awt.*;
public class GLetter extends GCompound{
private static final int ONE_THIRD = 30;
private static final int ROW_2_HEIGHT = 40;
private GArc[] arc = new GArc[4];
private GLine[] line = new GLine[24];
public GLetter(String s){
line[0] = new GLine(0,0, ONE_THIRD, 0);
line[1] = new GLine(ONE_THIRD,0, ONE_THIRD*2, 0);
line[2] = new GLine(ONE_THIRD*2,0, ONE_THIRD*3, 0);
line[3] = new GLine(0,0, 0,ONE_THIRD);
line[4] = new GLine(ONE_THIRD,0, ONE_THIRD, ONE_THIRD);
line[5] = new GLine(ONE_THIRD*2,0, ONE_THIRD*2, ONE_THIRD);
line[6] = new GLine(ONE_THIRD*3,0, ONE_THIRD*3, ONE_THIRD);
line[7] = new GLine(0,ONE_THIRD, ONE_THIRD*2, ONE_THIRD);
line[8] = new GLine(ONE_THIRD,ONE_THIRD, ONE_THIRD*2, ONE_THIRD);
line[9] = new GLine(ONE_THIRD*2,ONE_THIRD, ONE_THIRD*3, ONE_THIRD);
line[10] = new GLine(0,ONE_THIRD, 0, ONE_THIRD+ROW_2_HEIGHT);
line[11] = new GLine(ONE_THIRD, ONE_THIRD, ONE_THIRD, ONE_THIRD+ROW_2_HEIGHT);
line[12] = new GLine(ONE_THIRD*2,ONE_THIRD, ONE_THIRD*2, ONE_THIRD+ROW_2_HEIGHT);
line[13] = new GLine(ONE_THIRD*3,ONE_THIRD, ONE_THIRD*3, ONE_THIRD+ROW_2_HEIGHT);
line[14] = new GLine(0, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD, ONE_THIRD+ROW_2_HEIGHT);
line[15] = new GLine(ONE_THIRD, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD*2, ONE_THIRD+ROW_2_HEIGHT);
line[16] = new GLine(ONE_THIRD*2, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD*3, ONE_THIRD+ROW_2_HEIGHT);
line[17] = new GLine(0, ONE_THIRD+ROW_2_HEIGHT, 0, ONE_THIRD*2+ROW_2_HEIGHT);
line[18] = new GLine(ONE_THIRD, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD, ONE_THIRD*2+ROW_2_HEIGHT);
line[19] = new GLine(ONE_THIRD*2, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD*2, ONE_THIRD*2+ROW_2_HEIGHT);
line[20] = new GLine(ONE_THIRD*3, ONE_THIRD+ROW_2_HEIGHT, ONE_THIRD*3, ONE_THIRD*2+ROW_2_HEIGHT);
line[21] = new GLine(0,ONE_THIRD*2+ROW_2_HEIGHT, ONE_THIRD, ONE_THIRD*2+ROW_2_HEIGHT);
line[22] = new GLine(ONE_THIRD, ONE_THIRD*2+ROW_2_HEIGHT, ONE_THIRD*2, ONE_THIRD*2+ROW_2_HEIGHT);
line[23] = new GLine(ONE_THIRD*2,ONE_THIRD*2+ROW_2_HEIGHT, ONE_THIRD*3, ONE_THIRD*2+ROW_2_HEIGHT);
for(int i = 0; i<line.length; i++){
add(line[i]);
line[i].setColor(Color.BLACK);
line[i].setVisible(false);
}
arc[0] = new GArc(getWidth(), getHeight(), 106.699, 49.341);
arc[1] = new GArc(getWidth(), getHeight(), 23.96, 49.341);
arc[2] = new GArc(getWidth(), getHeight(), -23.96, -49.341);
arc[3] = new GArc(0,0,getWidth(), getHeight(), -106.699, -49.341);
for(int i = 0; i<arc.length; i++){
add(arc[i],0,0);
arc[i].setColor(Color.BLACK);
arc[i].setVisible(false);
}
paintLetter(s);
}
private void paintLetter(String s){
if (s.equalsIgnoreCase("l")){
turnOn(line[3]);
turnOn(line[10]);
turnOn(line[17]);
turnOn(line[21]);
turnOn(line[22]);
turnOn(line[23]);
}
else if(s.equalsIgnoreCase("o")){
for(int i = 0; i<4; ++i){
turnOn(arc[i]);
}
turnOn(line[1]);
turnOn(line[10]);
turnOn(line[13]);
turnOn(line[22]);
}
}
private void turnOn(GObject g){
g.setVisible(true);
}
}
I created a class (GLetter.java) with arrays for GArc and GLine objects. They are positioned in certain ways thereby turning certain Glines and/or GArcs on or off (changing visiblity) would create a pattern for a letter. This Gletter uses the if/else statements to determine which pattern to create - this makes me feel my code is too long.
There is another class (DrawInitials.java) that simulates a GraphicsProgram and allows the user to pass certain letters as arguments to the GLetter object. I've used 'L' and 'O' as examples.
However, I posted this because I'm not sure I'm using the right approach. That's why I need your help.
I feel MY CODE IS TOO LONG!
The code above is not the complete project...it only draws letters 'L' and 'O' for now.
© Programmers or respective owner